<?php

	/**
	 * @see Bike_Js_Abstract
	 */
	require_once 'Bike/Js/Abstract.php';
	
	
	class Bike_Js_ClassName extends Bike_Js_Abstract
	{
        protected $_className = array();

        public function makeOptions(array $Args=null)
		{
			$options = $Args;
			$argsCount = sizeof($Args);
			if($argsCount)
			{
				if($argsCount == 1 && is_array($Args[0]) && isset($Args[0]['className']))
				{
					$options = $Args[0];
				}
				elseif($argsCount >= 1)
				{
					$options = array(
						'className' => $Args[0],
					);
				}
			}
			return $options;
		}

        protected function getClassName()
        {        	return (array) $this->_className;
        }

        protected function setClassName($ClassName)
        {        	if(!is_array($ClassName))
        	{        		$ClassName = array($ClassName);
        	}
        	$className = $this->js()->getClassName(join('_', $ClassName));
        	$this->_className = $ClassName;
        	return $this;
        }

        protected function getClassNameObject()
        {        	return $this->js()->getClonedObjectInstance(join('_', $this->getClassName()));
        }

        public function _getObj()
        {        	return $this->getClassNameObject();
        }

        public function __get($Name)
		{
			try
			{				$className = $this->getClassName();
				array_push($className, $Name);
				$this->className = $className;
				return $this;
			}
			catch(Bike_Js_Exception $e)
			{				$result = $this->getClassNameObject()->$Name;
				if(is_null($result))
				{
					$obj = $this->js()->getClonedObjectInstance('Variable');
					$className = join('_', $this->getClassName());
					$result = $obj->$className->$Name;
				}
				return $result;
			}
		}

		public function __call($MethodName, array $Args=null)
		{			try
			{
				$className = $this->getClassName();
				array_push($className, $MethodName);
				$this->className = $className;				
				return $this->js()->getClonedObjectInstance(join('_', $this->getClassName()))->setArgsOptions($Args);
			}
			catch(Bike_Js_Exception $e)
			{
				try 
				{
					return call_user_func_array(array($this->getClassNameObject(), $MethodName), $Args);
				}
				catch(Bike_Js_Exception $e)
				{
					$obj = $this->js()->getClonedObjectInstance('Variable');
					$className = join('_', $this->getClassName());
					return call_user_func_array(array($obj->$className, $MethodName), $Args);					
				}
			}
		}

		public function __set($Name, $Value)
		{
			if($Name == 'className')
			{
				$this->setClassName($Value);
			}
			return $this;
		}

		public function _toString()
		{
			return (string) $this->getClassNameObject();
		}

		public function toJson()
		{
			return $this->getClassNameObject()->toJson();
		}
	}
